﻿using System.Net;
using System.IO;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Text;
using System.Linq;

namespace SMS_Gateway
{

    abstract public class HTTPGateway : Gateway
    {
        protected abstract String Prefix { get; }

        protected string BuildQueryString(string prefix, KeyValuePair<string, string>[] fields)
        {
            StringBuilder queryStringBuilder = new StringBuilder();
            queryStringBuilder.Append(prefix);
            queryStringBuilder.Append('?');

            string[] fieldequalvalueStringArr = (from x in fields let newS = (x.Key + "=" + x.Value) select newS).ToArray();

            queryStringBuilder.Append(String.Join("&", fieldequalvalueStringArr));

            //for
            //queryStringBuilder.Append

            return queryStringBuilder.ToString();

        }

        protected string HTTPSend(string sHTTPInputData)
        {

            string sResult;                         //-- the result to be returned from this function
            HttpWebResponse httpResponse = null;    //-- the response buffer used after the Request (HTTP GET) operation


            try
            {

                //-- create the HTTP GET request
                HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(sHTTPInputData);
                httpReq.Timeout = 120000;

                //-- now get the response


                httpResponse = (HttpWebResponse)httpReq.GetResponse();

                StreamReader input = new StreamReader(httpResponse.GetResponseStream());

                sResult = input.ReadToEnd();

                input.Close();
                input.Dispose();

                //input.Close();

            }

            catch (Exception e)
            {
                //-- something went wrong. Return result.
                sResult = "HTTP Error: " + e.Message;
            }

            finally
            {
                //-- clean up
                if (httpResponse != null)
                    httpResponse.Close();
            }

            //-- return result
            return sResult;
        }

        public AsyncCallback resCallBack { get; set; }


        public HTTPGateway(string senderID)
            : base(senderID)
        {
        }


        public abstract override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode);

        public override bool IsConnected
        {
            get
            {
                try
                {
                    HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(Prefix);

                    HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
                    if (HttpStatusCode.OK == rspFP.StatusCode)
                    {
                        // HTTP = 200 - Internet connection available, server online
                        rspFP.Close();
                        return true;
                    }
                    else
                    {
                        // Other status - Server or connection not available
                        rspFP.Close();
                        return false;
                    }
                }
                catch (WebException)
                {
                    // Exception - connection not available
                    return false;
                }

            }
        }



    }
}